package cn.zt.service.impl;

import cn.zt.common.Result;
import cn.zt.domain.Balance;
import cn.zt.domain.BankCardType;
import cn.zt.service.BalanceService;
import cn.zt.mapper.BalanceMapper;
import cn.zt.mapper.BankCardTypeMapper;
import cn.zt.service.BankCardTypeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.zt.domain.Bankcard;
import cn.zt.service.BankcardService;
import cn.zt.mapper.BankcardMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author 86182
* @description 针对表【bankcard(银行卡表
)】的数据库操作Service实现
* @createDate 2024-09-03 19:10:48
*/
@Service
public class BankcardServiceImpl extends ServiceImpl<BankcardMapper, Bankcard>
    implements BankcardService {

    @Resource
    private BankCardTypeService bankCardTypeService;
    @Autowired
    private BalanceService balanceService;

    @Resource
    private BankcardMapper bankcardMapper;

    @Resource
    private BankCardTypeMapper bankCardTypeMapper;

    @Resource
    private BalanceMapper balanceMapper;

    @Override
    public Result selectByNickname(String name) {
        List<Bankcard> list = this.list(Wrappers.<Bankcard>lambdaQuery().eq(Bankcard::getNickname, name));
        List<BankCardType> list1 = new ArrayList<>();
        for (Bankcard bankcard : list) {
            BankCardType bankCardType = bankCardTypeService.selectById(bankcard.getId());
            list1.add(bankCardType);
        }
        for (BankCardType bankCardType : list1) {
            if (bankCardType.getAccountCardTypeId() == 1) {
                return Result.error();
            }
        }
        return Result.success();
    }

    //根据银行卡号查询银行卡信息
    @Override
    public Bankcard selectByBankCard(String bankCard) {
        Bankcard bankcard = this.getOne(Wrappers.<Bankcard>lambdaQuery().eq(Bankcard::getBankCard, bankCard));
        return bankcard;
    }

    //根据名字查银行卡号
    @Override
    public List<Bankcard> selectBankCardByName(String nickname) {
        List<Bankcard> list = this.list(Wrappers.<Bankcard>lambdaQuery().eq(Bankcard::getNickname, nickname));
        return list;
    }

    @Override
    public int addBankcard(Bankcard bankcard) {
        this.save(bankcard);
        return bankcard.getId();
    }

    //根据银行卡修改余额
    @Override
    public int updateBalance(String bankcard, Integer money) {
        Bankcard bankcard1 = this.getOne(Wrappers.<Bankcard>lambdaQuery().eq(Bankcard::getBankCard, bankcard));
        if (bankcard1 != null) {
            BankCardType bankCardType = bankCardTypeService.getOne(Wrappers.<BankCardType>lambdaQuery().eq(BankCardType::getBankcardId, bankcard1.getId()));
            if (bankCardType != null) {
                Balance balance = new Balance();
                balance.setId(bankCardType.getBalanceId());
                balance.setBalanceNum(money);
                boolean b = balanceService.updateById(balance);
                return b ? 1 : 0;
            }
        }
        return 0;
    }

    @Override
    public List<Bankcard> selectByBankCardname(String nickname) {
        List<Bankcard> list = this.list(Wrappers.<Bankcard>lambdaQuery().eq(Bankcard::getNickname, nickname));
        return list;
    }

    @Override
    public Result selectByThree(Bankcard bankcard) {
        LambdaQueryWrapper<Bankcard> wrapper = Wrappers.<Bankcard>lambdaQuery().eq(Bankcard::getBankCard, bankcard.getBankCard())
                .eq(Bankcard::getNickname, bankcard.getNickname())
                .eq(Bankcard::getPhone, bankcard.getPhone());
        Bankcard one = this.getOne(wrapper);
        return Result.success(one);
    }

    @Override
    public Result getCardAll(String nickname) {
        List<Bankcard> bankcards = bankcardMapper.selectList(Wrappers.<Bankcard>lambdaQuery().eq(Bankcard::getNickname, nickname));
        return Result.success(bankcards);
    }

    @Override
    public Bankcard getBankCard(String bankCard) {
        Bankcard bankcard = bankcardMapper.selectOne(Wrappers.<Bankcard>lambdaQuery().eq(Bankcard::getBankCard, bankCard));
        if (bankcard != null) {
            BankCardType bankCardType = bankCardTypeMapper.selectOne(Wrappers.<BankCardType>lambdaQuery().eq(BankCardType::getBankcardId, bankcard.getId()));
            if (bankCardType.getBalanceId() != null) {
                Balance balance = balanceMapper.selectOne(Wrappers.<Balance>lambdaQuery().eq(Balance::getId, bankCardType.getBalanceId()));
                if (balance != null) {
                    bankcard.setBalance(balance.getBalanceNum());
                }
            }
        }
        return bankcard;
    }

    @Override
    public int jiBalance(String bankCard, Integer money) {
        Bankcard bankcard = bankcardMapper.selectOne(Wrappers.<Bankcard>lambdaQuery().eq(Bankcard::getBankCard, bankCard));
        if (bankcard != null) {
            BankCardType bankCardType = bankCardTypeMapper.selectOne(Wrappers.<BankCardType>lambdaQuery().eq(BankCardType::getBankcardId, bankcard.getId()));
            if (bankCardType.getBalanceId() != null) {
                Balance balance = balanceMapper.selectOne(Wrappers.<Balance>lambdaQuery().eq(Balance::getId, bankCardType.getBalanceId()));
                if (balance != null) {
                    balance.setBalanceNum(balance.getBalanceNum() - money);
                }
                int i = balanceMapper.updateById(balance);
                return i;
            }
        }
        return 0;
    }

    @Override
    public int jaBalance(String bankCard, Integer money) {
        Bankcard bankcard = bankcardMapper.selectOne(Wrappers.<Bankcard>lambdaQuery().eq(Bankcard::getBankCard, bankCard));
        if (bankcard != null) {
            BankCardType bankCardType = bankCardTypeMapper.selectOne(Wrappers.<BankCardType>lambdaQuery().eq(BankCardType::getBankcardId, bankcard.getId()));
            if (bankCardType.getBalanceId() != null) {
                Balance balance = balanceMapper.selectOne(Wrappers.<Balance>lambdaQuery().eq(Balance::getId, bankCardType.getBalanceId()));
                if (balance != null) {
                    balance.setBalanceNum(balance.getBalanceNum() + money);
                }
                int i = balanceMapper.updateById(balance);
                return i;
            }
        }
        return 0;
    }

    @Override
    public Bankcard selectBankCardByNickName(String nickname) {
        List<BankCardType> bankCardTypes = bankCardTypeService.selectOneType();
        for (BankCardType bankCardType : bankCardTypes) {
            Bankcard one = this.getOne(Wrappers.<Bankcard>lambdaQuery().eq(Bankcard::getNickname, nickname).eq(Bankcard::getId, bankCardType.getBankcardId()));
            if (one != null) {
                return one;
            }
        }
        return null;
    }


    @Override
    public Integer findId(String bankCard) {
        Bankcard one = this.getOne(Wrappers.<Bankcard>lambdaQuery().eq(Bankcard::getBankCard, bankCard));
        Integer integer = bankCardTypeService.selectA(one.getId());
        return integer;
    }

    //根据昵称查询银行卡
    @Override
    public Result findCards(String nickname) {
        List<Bankcard> list = this.list(Wrappers.<Bankcard>lambdaQuery().eq(Bankcard::getNickname, nickname));
        //根据银行卡查余额
        for (Bankcard bankcard : list) {
            if (bankcard != null && bankcard.getBankCard() != null){
                Bankcard selectbalance = balanceService.selectbalance(bankcard.getBankCard());
                if (selectbalance != null && selectbalance.getBalance() != null) {
                    bankcard.setBalance(selectbalance.getBalance());
                }
            }
        }
        return Result.success(list);
    }


    //个人账户
//    @Override
//    public Result myCount(String nickname) {
//        List<Bankcard> bankcardList = this.list(Wrappers.<Bankcard>lambdaQuery().eq(Bankcard::getNickname, nickname));
////        通过银行卡号查找余额
//        for (Bankcard bankcard : bankcardList) {
//            Bankcard selectbalance = balanceService.selectbalance(bankcard.getBankCard());
//            if (selectbalance != null && selectbalance.getBalance()!= null){
//                bankcard.setBalance(selectbalance.getBalance());
//            }
//        }
//        return Result.success(bankcardList);
//
//
//    }

//    @Override
//    public Result findBankcardBalance(String nickname) {
//        List<Bankcard> list = this.list(Wrappers.<Bankcard>lambdaQuery().eq(Bankcard::getNickname, nickname));
//        ArrayList<String> strings = new ArrayList<>();
//        //根据银行卡
//        for (Bankcard bankcard : list) {
//            if (bankcard != null && bankcard.getBankCard() != null){
//                Bankcard selectbalance = balanceService.selectbalance(bankcard.getBankCard());
//                if (selectbalance != null && selectbalance.getBalance() != null) {
//                    bankCardVo.setBankcard(bankcard.getBankCard());
//                    bankCardVo  .setBalance(selectbalance.getBalance());
//                }
//            }
//        }
//        return Result.success(bankCardVo);
//    }
//        BankCardVo bankcardVo = new BankCardVo();
//        List<Bankcard> list = this.list(Wrappers.<Bankcard>lambdaQuery().eq(Bankcard::getNickname, nickname));
//        List<String> bankcards = new ArrayList<>();
//        for (Bankcard bankcard : list) {
//           if ( bankcard != null){
//              bankcards.add(bankcard.getBankCard());
//           }
//        }
//        bankcardVo.setBankcard(bankcards);
//        for (String bankcard : bankcards) {
//            if (bankcard != null){
//                //根据银行卡号查余额
//                Bankcard selectbalance = balanceService.selectbalance(bankcard);
//                if (selectbalance != null && selectbalance.getBalance() != null){
//                    bankcardVo.setBalance(selectbalance.getBalance());
//                }
//            }
//        }
//        return Result.success(bankcardVo);
//    }
}



